<?php

/**
 * User:ayou chow
 * Date: 2020-02-01
 * Time: 14:33
 */

namespace app\common\model;


use think\Model;
use think\Db;

class LiveModel extends Model
{
    protected $connection = 'db_live';

    public function anchor()
    {
        return $this->hasOne('UserModel', 'id', 'anchorid')->with('profile')->field("id, nick_name, avatar, is_anchor");
    }

    public function user()
    {
        return $this->hasOne('UserModel', 'id', 'anchorid');
    }

    public function category()
    {
        return $this->hasOne('LiveCategoryModel', 'id', 'categoryid');
    }

    public function matches()
    {
        return $this->hasOne(MatchesScheduleModel::class, 'match_id', 'matchid');
    }


    public static function matchInfo($matchId, $ballType)
    {
        $result = [];
        $match  = Db::query("select  m.home_id,m.away_id,e.name_zh from db_{$ballType}_match as m left join db_{$ballType}_matchevent as e on m.competition_id = e.id where m.id = {$matchId} ");
        if (isset($match[0])) {
            $result         = $match[0];
            $result['team'] = '';
            $teamInfo       = Db::query("select name_zh from db_basket_team where id in ({$result["home_id"]},{$result["away_id"]})");
            if (count($teamInfo) == 2) {
                $result['team'] = $teamInfo[0]['name_zh'] . 'VS' . $teamInfo[1]['name_zh'];
            }
        }
        return $result;
    }

    public static function auto_live_ids()
    {
        $ids = [
            283391,
            283390,
            283389,
            283388,
            283387,
            283386,
            283385,
            283384,
            283383,
            283382,
            283381,
            283380,
            283379,
            283378,
            283377,
            283376,
            283375,
            283374,
            283373,
            283372,
            283371,
            283370,
            283369,
            283368,
            283367,
            283366,
            283365,
            283364,
            283363,
            283362,
            283361,
            283360,
            283359,
            283358,
            283357,
            283356,
            283355,
            283354,
            283353,
            283352,
            283351,
            279940,
            279939,
            279937,
            279936,
            279935,
            279934,
            279933,
            279932,
            279931,
            279930,
            279929,
            279928,
            279953,
            279952,
            279951,
            279950,
            279949,
            279948,
            279947,
            279946,
            279945,
            279944,
            279943,
            279942,
            279941,
            279958,
            279957,
            279956,
            279955,
            279954,
            264549,
            264550,
            264551,
            264552,
            264553,
            264554,
            264555,
            264556,
            264557,
            264558,
            262283,
            262284,
            262285,
            262286,
            262287,
            262288,
            262289,
            262290,
            262291,
            262292,
            264539,
            264540,
            264541,
            264542,
            264543,
            264544,
            264545,
            264546,
            264547,
            264548,
        ];

        return implode(',', $ids);
    }
}
